<?php

namespace app\admin\validate;

use app\admin\model\Admin;
use app\admin\model\Menu;
use think\Validate;

class AdminForm extends Validate
{
    protected $rule = [

        'nickname|昵称'  =>  'require|min:2|max:200',
        'username|用户名'  =>  'require|min:5|max:25|alphaNum',
        'password|密码' =>  'require|max:128',
//        'verity_code|验证码'=>'require|captcha',
    ];
    protected $message  =   [
        'username.require' => '用户名必须!',
        'username.max'     => '名称最多不能超过25个字符',
        'username.alphaNum'  => '用户名只能是数字字母！',
        'password.number'   => '密码必须',
    ];

    public function editUser($data){
        require_once  mooshroom;
        $hashObj = new \mooshroom\details\crypt\Hash();
        $data['password'] = $hashObj->Encryption($data['password']);
        $data['role_name'] = Admin::getRoleName($data['role_id']);
        if((new Admin())->editUser($data['id'],$data)){
            return ['code'=>0,'msg'=>'添加成功!'];
        }else{
            return ['code'=>20011,'msg'=>'添加失败!'];
        }
    }
    public function addUser($data){
        require_once  mooshroom;
        $hashObj = new \mooshroom\details\crypt\Hash();
        $data['password'] = $hashObj->Encryption($data['password']);
        $data['role_name'] = Admin::getRoleName($data['role_id']);
        $data['role_name'] = Admin::getRoleName($data['role_id']);
        if((new Admin())->getUserNameById($data['username'])){
            return ['code'=>20002,'msg'=>'用户名已存在!'];
        }

        if((new Admin())->addUser($data)){
            return ['code'=>0,'msg'=>'添加成功!'];
        }else{
            return ['code'=>20011,'msg'=>'添加失败!'];
        }
    }
}